package com.can.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.can.Mapper.UserMapper;

import com.can.Utils.JwtUtil;
import com.can.domain.vo.CommonVo;
import com.can.domain.vo.UserInfo;
import com.can.domain.vo.WodeVo;
import com.can.service.UserService;
import com.can.domain.po.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class serviceImpl  extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    JwtUtil jwtUtil;

    @Override
    public CommonVo login(String name, String password) {
        // 构造查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name) // 查询用户名
                .eq("password", password); // 查询密码

        // 查询用户
        User user = userMapper.selectOne(queryWrapper);

        // 判断用户是否存在
        if (user == null) {
            return new CommonVo<>(401, "用户名或密码错误", null);
        }
        System.out.println("秘钥:"+jwtUtil.outmiyao());
        String token=jwtUtil.generateToken(user.getId(), Integer.parseInt(user.getPower()),user.getUsername());
        System.out.println("生成的token:"+token);
        UserInfo userInfo = new UserInfo(user.getId(),user.getPower(),token);
        // 登录成功，返回用户信息或令牌
        return new CommonVo<>(200, "登录成功",userInfo);

    }

    @Override
    public CommonVo register(String name, String password) {
        // 构造查询条件
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", name) ;// 查询用户名
        // 查询用户
        User user = userMapper.selectOne(queryWrapper);

        // 判断用户是否存在
        if (user != null) {
            System.out.println("已存在");
            return new CommonVo<>(409, "用户已经存在", null);
        }
        User newuser=new User();
        newuser.setUsername(name);
        newuser.setPassword(password);
        try {
            userMapper.insert(newuser);
            return new CommonVo<>(200, "注册成功","");
        }catch (Exception e) {
            e.printStackTrace();
            return new CommonVo<>(500, "注册失败","");

        }


    }

    @Override
    public CommonVo wode(int id) {
        try{
//            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("id", id) ;// 查询用户名
            // 查询用户
            WodeVo wode = userMapper.wode(id);
            System.out.println("用户查询结果:"+wode);
            return new CommonVo<>(200, "查询成功",wode);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonVo<>(400, "查询失败","");
        }

    }

//@RocketMQMessageListener(
//    topic = "game_experience_topic",
//    consumerGroup = "experience_group"
//)
    @Override
    public CommonVo jia3(int id) {
        try{
            userMapper.updatePoints(id);
            return new CommonVo<>(200, "操作成功",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonVo<>(400, "操作失败","");
        }
    }
}
